Security News
GitHub Removes Malicious Pull Requests Targeting Open Source Repositories
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
postcss-calc-ast-parser
Advanced tools
Parse the calc()
function defined in CSS and convert it to AST.
There are already various parsers of CSS and values defined in CSS.
However, these parser do not have the information of calc function AST. Also, it may not be possible to use it combination with SCSS or Less.
postcss-calc-ast-parser
is a parser specialized for the calc()
(max
, min
and clamp
) functions.
We are aiming to work even when used in combination with SCSS or Less.
npm install --save-dev postcss-calc-ast-parser
const calcAstParser = require("postcss-calc-ast-parser")
const parsed = calcAstParser.parse("calc(100%/3 - 2*1em - 2*1px)")
For example, parsing the value calc(100%/3 - 2*1em - 2*1px)
will return the following:
{
"type": "Root",
"nodes": [
{
"type": "Function",
"name": "calc",
"nodes": [
{
"type": "MathExpression",
"left": {
"type": "MathExpression",
"left": {
"type": "Percentage",
"value": 100,
"unit": "%"
},
"operator": "/",
"right": {
"type": "Number",
"value": 3
}
},
"operator": "-",
"right": {
"type": "MathExpression",
"left": {
"type": "MathExpression",
"left": {
"type": "Number",
"value": 2
},
"operator": "*",
"right": {
"type": "Length",
"value": 1,
"unit": "em"
}
},
"operator": "-",
"right": {
"type": "MathExpression",
"left": {
"type": "Number",
"value": 2
},
"operator": "*",
"right": {
"type": "Length",
"value": 1,
"unit": "px"
}
}
}
}
]
}
]
}
To know more about certain nodes in produced AST, please go AST docs.
const calcAstParser = require("postcss-calc-ast-parser")
const parsed = calcAstParser.parse("calc(100% - 20px)")
Parse the given source code.
calcAstParser.parse(
code: string,
options?: {
/**
* Allow inline comments. default `true`
*/
allowInlineCommnets: boolean
}
): AST.Root
Stringifies the given node.
calcAstParser.stringify(node: AST.Node): string
Get the resolved type of the given math expression.
calcAstParser.getResolvedType(expr: AST.MathExpression):
| "Number" | "Length" | "Angle" | "Time" | "Frequency" | "Resolution" | "Percentage" | "Flex"
| "Unknown" // Is includes unknown values and SCSS interpolation etc.
| "invalid" // Type can not be resolved.
Returns the calculated value of MathExpression
. Returns null
if it can not be resolved.
calcAstParser.reduceExpression(expr:
| AST.MathExpression
| AST.Root
| AST.FunctionNode
| AST.NumberValue
| AST.LengthValue
| AST.AngleValue
| AST.TimeValue
| AST.FrequencyValue
| AST.ResolutionValue
| AST.PercentageValue
| AST.FlexValue
):
| {
value: number,
type: "Number" | "Length" | "Angle" | "Time" | "Frequency" | "Resolution" | "Percentage" | "Flex",
unit?: string
}
| null
Walks each node of the given type inside parsed.nodes.
parsed.walk(type: string | RegExp, callback: (node) => boolean | void): boolean | void
FAQs
Parses calc of css and converts it to AST.
The npm package postcss-calc-ast-parser receives a total of 42,839 weekly downloads. As such, postcss-calc-ast-parser popularity was classified as popular.
We found that postcss-calc-ast-parser demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.